package com.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.catalina.connector.Request;
import org.apache.jasper.tagplugins.jstl.core.ForEach;


import db.DBManager;
import db.QueryDataResult;

public class UserEntity {
	public ArrayList<UserBean> selectALL(String keyName) {
		ArrayList<UserBean> alist= new ArrayList<UserBean>();
		String sqlString;
		if (keyName!=null&&!keyName.equals("")) {
			sqlString="select id,name,sex,age from user where name like '%"+keyName+"%'";
		}else {
			 sqlString="select id,name,sex,age from user";
		}
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						alist.add(new UserBean(rsSet.getString("name"), rsSet.getString("sex"), rsSet.getString("age"),
								rsSet.getString("id")));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
		return alist;
	}
	
	public UserBean userDetail(String id) {
		UserBean userBean= null;
		String sqlString="select id,name,sex,age from user where id='"+id+"'";
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						userBean=new UserBean(rsSet.getString("name"), rsSet.getString("age"), rsSet.getString("sex"),
								rsSet.getString("id"));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
			return userBean;
	}
	
	public int addUser(UserBean userBean) {
		String sqlString="insert into user(id,name,sex,age) values ('"+userBean.getId()+"','"+userBean.getName()
		  +"','"+userBean.getSex()+"','"+userBean.getAge()+"')";
		 int result= DBManager.excuteUpdate(sqlString);
		return result;
	}
	public int updateUser(UserBean userBean) {
		 String sqlString="update user set name='"+userBean.getName()+"',age='"+userBean.getAge()+"',sex='"+userBean.getSex()+"'"
		 		+ " where id='"+userBean.getId()+"'";
		 		
			     
		   int result= DBManager.excuteUpdate(sqlString);
			      return result;
	}
	public int deleteUser(String id) {
		String sqlString="DELETE from user WHERE id='"+id+"'";
		int result=DBManager.excuteUpdate(sqlString);
		return result;
	}

}
